﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 生产计划批量打印工具
{
    public partial class frmJhTypeConfig : Form
    {
        string strConnectionString = "Data Source=@ServerAddress;Initial Catalog=@DatabaseName;Persist Security Info=True;User ID=sa;Password=856518armyman";
        SqlTools.SqlTools  sqt;

        public frmJhTypeConfig()
        {
            InitializeComponent();
            sqt = new SqlTools.SqlTools(strConnectionString.Replace("@ServerAddress", Properties.Settings.Default.ServerAddress)
                                                                                        .Replace("@DatabaseName", "DefualtDatabase"));
            LoadJhType();
        }

        private void LoadJhType()
        {
            lstTypes.Items.Clear();
            string strSelectJhType = "select ID 分类ID,TypeName 分类名称,停用 from 计划分类表 order by TypeName";
            DataTable tmpdt = sqt.GetDataTable(strSelectJhType);
            if (tmpdt.Rows.Count > 0)
            {
                int[] TypeIDs = new int[tmpdt.Rows.Count];
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    TypeIDs[i] = (int)tmpdt.Rows[i]["分类ID"];
                    if (GetBoolean(tmpdt.Rows[i]["停用"]))
                    {
                        lstTypes.Items.Add("*" + tmpdt.Rows[i]["分类名称"].ToString());
                    }
                    else
                    {
                        lstTypes.Items.Add(tmpdt.Rows[i]["分类名称"].ToString());
                    }
                }
                lstTypes.Tag = TypeIDs;
            }
        }

        private bool GetBoolean(object tmpobj)
        {
            try
            {
                return Convert.ToBoolean(tmpobj);
            }
            catch
            {
                return false;
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString();
            }
            catch
            {
                return string.Empty;
            }
        }

        private void lstTypes_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index >= 0  )
            {
                if (e.Index >= 0)
                {
                    e.DrawBackground();
                    Brush mybsh = Brushes.Black;
                    // 判断是什么类型的标签  
                    if (lstTypes.Items[e.Index].ToString().Contains("*"))
                    {
                        mybsh = Brushes.Red;
                    }
                    // 焦点框  
                    e.DrawFocusRectangle();
                    //文本   
                    e.Graphics.DrawString(lstTypes .Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
                }  
            }
        }

        private void lstTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            tvLogics.Nodes.Clear();
            txtLogic .Clear ();
            if (lstTypes.SelectedItem != null)
            {
                AddNodes(null, string.Empty, GetString(((int[])lstTypes.Tag).GetValue (lstTypes .SelectedIndex )));
                txtLogic.Text = GetLogic();
            }
        }

        private void AddNodes(TreeNode tnParentNode,string strParentNodeID,string strTypeID)
        {
            string strSelectTypeLogics = "select NodeID 节点ID,ParentNodeID 父节点ID,NodeName 节点名称,LastLogic 节点逻辑,Field 字段,FieldLogic 字段逻辑,Value 值 from 计划分类逻辑表 where TypeID=@TypeID and ParentNodeID='@ParentNodeID' order by NodeID";
            if (strParentNodeID.Equals(string.Empty))
            {
                strSelectTypeLogics = strSelectTypeLogics.Replace("@TypeID", strTypeID)
                                                                                 .Replace("='@ParentNodeID'", " is null");
            }
            else
            {
                strSelectTypeLogics = strSelectTypeLogics.Replace("@TypeID", strTypeID)
                                                                                 .Replace("@ParentNodeID", strParentNodeID );
            }
            DataTable tmpdt = sqt.GetDataTable(strSelectTypeLogics);
            if (tmpdt.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdt.Rows.Count; i++)
                {
                    string strNodeID = GetString(tmpdt.Rows[i]["节点ID"]);
                    TreeNode tmptn = new TreeNode();
                    if (GetString(tmpdt.Rows[i]["节点名称"]).Equals(string.Empty))
                    {
                        tmptn.Text = GetString(tmpdt.Rows[i]["节点逻辑"]) + " " + GetString(tmpdt.Rows[i]["字段"]) + " " + GetString(tmpdt.Rows[i]["字段逻辑"]) + " " + GetString(tmpdt.Rows[i]["值"]);
                    }
                    else
                    {
                        tmptn.Text = GetString(tmpdt.Rows[i]["节点名称"]);
                    }
                    tmptn.Tag = (GetString(tmpdt.Rows[i]["节点逻辑"]) + " " + GetString(tmpdt.Rows[i]["字段"]) + " " + GetString(tmpdt.Rows[i]["字段逻辑"]) + " '" + GetString(tmpdt.Rows[i]["值"])+"'").Replace("   ''",string.Empty ) ;
                    if (tnParentNode != null)
                    {
                        tnParentNode.Nodes.Add(tmptn);
                    }
                    else
                    {
                        tvLogics.Nodes.Add(tmptn);
                    }
                    AddNodes(tmptn, strNodeID, strTypeID);
                }
            }
        }

        private string GetLogic()
        {
            string strLogic = string.Empty;
            if (tvLogics.Nodes.Count > 0)
            {
                for (int i = 0; i < tvLogics.Nodes.Count; i++)
                {
                    string strSubLogic=string.Empty ;
                    if (tvLogics.Nodes[i].Nodes.Count > 0)
                    {
                        //有子节点
                        strLogic +=" "+ GetString(tvLogics.Nodes[i].Tag) + " (" + GetSubLogic(tvLogics.Nodes[i]) + ")";
                    }
                    else
                    {
                        //无子节点
                        strLogic += " " + GetString(tvLogics.Nodes[i].Tag);
                    }
                }
            }
            return strLogic;
        }

        private string GetSubLogic(TreeNode tnParentNode)
        {
            string strSubLogic = string.Empty;
            if (tnParentNode .Nodes .Count >0)
            {
                //遍历每个子节点
                for (int i = 0; i < tnParentNode.Nodes.Count; i++)
                {
                    if (tnParentNode.Nodes[i].Nodes.Count > 0)
                    {
                        //有子节点
                        strSubLogic += " (" + GetSubLogic(tnParentNode.Nodes[i]) + ")";
                    }
                    else
                    {
                        //无子节点
                        strSubLogic += " " + GetString(tnParentNode.Nodes[i].Tag);
                    }
                }
            }
            return strSubLogic;
        }

        private void btnDeleteType_Click(object sender, EventArgs e)
        {
            if (lstTypes.SelectedIndex >= 0)
            {
                if (MessageBox.Show("确认要删除选中的分类吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    string strDeleteType1 = "delete from 计划分类表 where ID='" + ((int[])lstTypes.Tag).GetValue(lstTypes.SelectedIndex).ToString() + "'";
                    string strDeleteType2 = "delete from 计划分类逻辑表 where TypeID='" + ((int[])lstTypes.Tag).GetValue(lstTypes.SelectedIndex).ToString() + "'";
                    sqt.DoCmdExecuteNonQuery(strDeleteType1);
                    sqt.DoCmdExecuteNonQuery(strDeleteType2);
                    LoadJhType();
                }
            }
        }
    }
}